/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;

public class Translation {

   private static Translation[] empty_array = new Translation[0];

   String       text;
   JavaType     type;
   boolean      not_null;
   boolean      consistent; // spojny, tzn. nie wymagajacy nawiasow np. literal, fn(b), ale nie np. 1+1
   Set<String>  modifiedVariables;

   Translation() {
      this.text       = null;
      this.type       = null;
      this.consistent = false;
      this.not_null   = false;
      modifiedVariables = Collections.emptySet();
   }

   Translation(String text, JavaType type, boolean consistent, boolean not_null) {
      // TODO: Usunac ta metode !
      this.text         = text;
      this.type         = type;
      this.consistent   = consistent;
      this.not_null     = not_null;
      modifiedVariables = Collections.emptySet();
   }

   Translation(String text, JavaType type, boolean consistent, boolean not_null, Set<String> modifiedVariables) {
      this.text              = text;
      this.type              = type;
      this.consistent        = consistent;
      this.not_null          = not_null;
      this.modifiedVariables = Collections.unmodifiableSet( modifiedVariables );
   }

   public static Translation[] eta() {
      return empty_array;
   }

   public String toString() {
      StringBuilder sb=new StringBuilder();
      return sb.append("<")
               .append(text)
               .append(", ")
               .append(type!=null ? type.name() : "null")
               .append(consistent ? ", consistent" : ", not consistent")
               .append(not_null ? ", not null" : ", nullable")
               .append(modifiedVariables!=null ? modifiedVariables.toString() : "null")
               .append(">")
               .toString();
   }

   String paren() {
      if(!consistent) {
         return "( "+ text +" )";
      }
      return text;
   }

   public static void makeConsistent(Translation[] translations, int index) {
      if( index>=0 && index<translations.length ) {
         Translation t = translations[index];
         if( !t.consistent ) {
            t.text       = "( "+ t.text +" )";
            t.consistent = true;
         }
      }
   }

   public static void makeConsistent(Translation[] translations, int[] indexes) {
      for( int i : indexes ) {
         makeConsistent(translations, i);
      }
   }
}
